<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务项目" prop="serviceName">
        <el-input
          v-model="queryParams.serviceName"
          placeholder="请输入服务项目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品数量" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入商品数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报价价格" prop="repairedPrice">
        <el-input
          v-model="queryParams.repairedPrice"
          placeholder="请输入报价价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="学校id" prop="schoolId">
        <el-input
          v-model="queryParams.schoolId"
          placeholder="请输入学校id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址id" prop="addressId">
        <el-input
          v-model="queryParams.addressId"
          placeholder="请输入地址id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="订单联系人" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入订单联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="经销商" prop="dealerName">
        <el-input
          v-model="queryParams.dealerName"
          placeholder="请输入经销商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:order:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="自增id" align="center" prop="id" /> -->
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="服务项目" align="center" prop="serviceName" />
      <!-- <el-table-column label="用户描述" align="center" prop="content" /> -->
      <el-table-column label="商品数量" align="center" prop="orderNum" />
      <el-table-column label="修复类型" align="center" prop="repairedType" />
      <el-table-column label="报价价格" align="center" prop="repairedPrice" />
      <!-- <el-table-column label="学校id" align="center" prop="schoolId" /> -->
      <!-- <el-table-column label="地址id" align="center" prop="addressId" /> -->
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="订单联系人" align="center" prop="phone" />
      <!-- <el-table-column label="用户id" align="center" prop="userId" /> -->
      <el-table-column label="经销商" align="center" prop="dealerName" />
      <el-table-column label="状态" align="center" prop="statusName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
              v-hasPermi="['system:order:query']"
            >查看</el-button>
          <el-button v-if="scope.row.status === '6'"
            size="mini"
            type="text"
            icon="el-icon-check"
            v-hasPermi="['system:order:edit']"
            @click="handleFinish(scope.row)"
          >完结订单</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px"  :disabled = "true">
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="服务项目" prop="serviceName">
          <el-input v-model="form.serviceName" placeholder="请输入服务项目" />
        </el-form-item>
        <el-form-item label="用户描述">
          <editor v-model="form.content" :min-height="192" :disabled = "true"/>
        </el-form-item>
        <el-form-item label="商品数量" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入商品数量" />
        </el-form-item>
        <el-form-item label="报价价格" prop="repairedPrice">
          <el-input v-model="form.repairedPrice" placeholder="请输入报价价格" />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="订单联系人" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入订单联系人" />
        </el-form-item>
        <el-form-item label="订单图片" prop="file">
          <div :style="`display:flex;flex-wrap:wrap;justify-content:space-between`">
            <div v-for="item in form.pFileInfoList" v-bind:key="item.id">
              <ImagePreview v-if="item.fileType === 'jpg' || item.fileType === 'png'" :style="`width:100px;height:100px;min-width:50px`" :src = "item.fileUrl"></ImagePreview>
              <VideoView v-else :style="`width:100px;height:100px;min-width:50px`" :src = "item.fileUrl"></VideoView>
            </div>
        </div>
        </el-form-item>
        <el-form-item label="经销商" prop="dealerName">
          <el-input v-model="form.dealerName" placeholder="请输入经销商id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/system/order";
import ImagePreview from "@/components/ImagePreview"
import VideoView from "@/components/VideoView"
export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        serviceName: null,
        content: null,
        orderNum: null,
        repairedType: null,
        repairedPrice: null,
        schoolId: null,
        addressId: null,
        address: null,
        phone: null,
        userId: null,
        dealerId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        schoolId: [
          { required: true, message: "学校id不能为空", trigger: "blur" }
        ],
        addressId: [
          { required: true, message: "地址id不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNo: null,
        serviceName: null,
        content: null,
        orderNum: null,
        repairedType: null,
        repairedPrice: null,
        schoolId: null,
        addressId: null,
        address: null,
        phone: null,
        userId: null,
        dealerId: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    handleFinish(row){
      this.$modal.confirm('是否确认完成订单？').then(function() {
        row.status = '5'
        return  updateOrder(row);
      }).then(() => {
        this.$modal.msgSuccess("修改成功");
          this.getList();
      }).catch(() => {});
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看订单";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
